package com.xuyiwei.xblog.modular.article.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.xuyiwei.xblog.business.article.result.article.input.CommentConditionVO;
import com.xuyiwei.xblog.business.article.result.article.out.CommentDto;
import com.xuyiwei.xblog.business.system.model.LoginLog;
import com.xuyiwei.xblog.business.system.model.OperationLog;
import com.xuyiwei.xblog.business.system.service.IMailService;
import com.xuyiwei.xblog.core.base.constant.enums.TemplateKeyEnum;
import com.xuyiwei.xblog.core.base.controller.BaseController;
import com.xuyiwei.xblog.core.base.response.Result;
import com.xuyiwei.xblog.core.common.constant.factory.PageFactory;
import com.xuyiwei.xblog.core.exception.BizExceptionEnum;
import com.xuyiwei.xblog.core.exception.GunsException;
import com.xuyiwei.xblog.core.page.PageInfoBT;
import com.xuyiwei.xblog.core.support.StrKit;
import com.xuyiwei.xblog.core.util.ToolUtil;
import com.xuyiwei.xblog.modular.system.warpper.LogWarpper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.xuyiwei.xblog.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.xuyiwei.xblog.business.article.model.Comment;
import com.xuyiwei.xblog.business.article.service.ICommentService;
import java.util.List;
import java.util.Map;

/**
 * 文章评论控制器
 *
 * @author fengshuonan
 * @Date 2018-10-29 17:26:53
 */
@Controller
@RequestMapping("/article/comment")
public class CommentController extends BaseController {

    private String PREFIX = "/article/comment/";

    @Autowired
    private ICommentService commentService;

    @Autowired
    private IMailService mailService;

    /**
     * 跳转到文章评论首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "comment.html";
    }

    /**
     * 跳转到添加文章评论
     */
    @RequestMapping("/comment_add")
    public String commentAdd() {
        return PREFIX + "comment_add.html";
    }

    /**
     * 跳转到文章回复页面
     * @param commentId
     * @param model
     * @return
     */
    @RequestMapping("/comment_reply/{commentId}")
    public String commentReply(@PathVariable Integer commentId,Model model) {
        Comment comment = commentService.selectById(commentId);
        model.addAttribute("item",comment);

        return PREFIX + "comment_reply.html";
    }

    /**
     * 跳转到文章审核页面
     * @param commentId
     * @param model
     * @return
     */
    @RequestMapping("/comment_check/{commentId}")
    public String commentCheck(@PathVariable Integer commentId,Model model) {
        Comment comment = commentService.selectById(commentId);
        model.addAttribute("item",comment);

        return PREFIX + "comment_check.html";
    }

    /**
     * 跳转到修改文章评论
     */
    @RequestMapping("/comment_update/{commentId}")
    public String commentUpdate(@PathVariable Integer commentId, Model model) {
        Comment comment = commentService.selectById(commentId);
        model.addAttribute("item",comment);
        LogObjectHolder.me().set(comment);
        return PREFIX + "comment_edit.html";
    }

    /**
     * 获取文章评论列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Result<PageInfoBT<CommentDto>> list(CommentConditionVO vo) {
        Result<PageInfoBT<CommentDto>> result = Result.instance();
        try{
            Page<CommentDto> page = new PageFactory<CommentDto>().defaultPage();
            List<CommentDto> list = commentService.findPageByCondition(page, vo, page.getOrderByField(), page.isAsc());
            page.setRecords(list);
            result.setData(super.packForBT(page));
        }catch (Exception e){
            result.setError("500","获取文章评论列表出错");
        }
        return result;
    }

    /**
     * 新增文章评论
     */
    @RequestMapping(value = "/reply")
    @ResponseBody
    public Result<Boolean> reply(Comment comment) {
        Result<Boolean> result = Result.instance();
        try{
            commentService.reply(comment);
            result.setData(true);
        }catch (GunsException e){
            result.setError("500",e.getMessage());
        }catch (Exception e){
            result.setError(BizExceptionEnum.REQUEST_NULL);
        }
        return result;
    }

    /**
     * 新增文章评论
     */
    @RequestMapping(value = "/check")
    @ResponseBody
    public Result<Boolean> check(Comment comment,String contentText,Boolean sendEmail) {
        Result<Boolean> result = Result.instance();
        try{
            commentService.updateById(comment);
            if(StrKit.notBlank(contentText)){
                comment.setContent(contentText);
                commentService.commentForAdmin(comment);
            }
            if(null != sendEmail && sendEmail){
                Comment commentDB = commentService.selectById(comment.getId());
                mailService.send(commentDB, TemplateKeyEnum.TM_COMMENT_AUDIT, true);
            }
            result.setData(true);
        }catch (GunsException e){
            result.setError("500",e.getMessage());
        }catch (Exception e){
            result.setError(BizExceptionEnum.REQUEST_NULL);
        }
        return result;
    }

    /**
     * 新增文章评论
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Result<Boolean> add(Comment comment) {
        Result<Boolean> result = Result.instance();
        try{
            commentService.insert(comment);
            result.setData(true);
        }catch (Exception e){
            result.setError(BizExceptionEnum.REQUEST_NULL);
        }
        return result;
    }

    /**
     * 删除文章评论
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Result<Boolean> delete(@RequestParam(required = true, value = "ids[]") List<Integer> ids) {
        Result<Boolean> result = Result.instance();
        try{
            if (ToolUtil.isEmpty(ids)) {
                throw new GunsException(BizExceptionEnum.REQUEST_NULL);
            }
            this.commentService.deleteBatchIds(ids);
        }catch (Exception e){
            result.setError(BizExceptionEnum.REQUEST_NULL);
        }
        return result;
    }

    /**
     * 修改文章评论
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Result<Boolean> update(Comment comment) {
        Result<Boolean> result = Result.instance();
        try{
            commentService.updateById(comment);
        }catch (Exception e){
            result.setError(BizExceptionEnum.REQUEST_NULL);
        }
        return result;
    }

    /**
     * 文章评论详情
     */
    @RequestMapping(value = "/detail/{commentId}")
    @ResponseBody
    public Result<Comment> detail(@PathVariable("commentId") Integer commentId) {
        Result<Comment> result = Result.instance();
        try{
            Comment comment = commentService.selectById(commentId);
            result.setData(comment);
        }catch (Exception e){
            result.setError(BizExceptionEnum.REQUEST_NULL);
        }
        return result;
    }
}
